<template>
  <div class="goods-image">
    <!-- 放大镜 -->
    <div
      v-show="!isOutside"
      class="large"
      :style="[
        {
          backgroundImage: `url(${mainImg[currIndex]})`,
          backgroundPositionX: `${bg.x}px`,
          backgroundPositionY: `${bg.y}px`
        }
      ]"
    ></div>
    <div class="middle" ref="target">
      <img :src="mainImg[currIndex]" alt="" />
      <!-- 蒙层容器 -->
      <div
        v-show="!isOutside"
        class="layer"
        :style="{ left: left + 'px', top: top + 'px' }"
      ></div>
    </div>
    <ul class="small">
      <li
        v-for="(img, i) in mainImg"
        :key="i"
        :class="{ active: currIndex === i }"
        @mouseenter="currIndex = i"
      >
        <img :src="img" alt="" />
      </li>
    </ul>
  </div>
</template>
<script>
import { reactive, ref, watch } from 'vue'
import { useMouseInElement } from '@vueuse/core'
export default {
  name: 'GoodImage',
  props: {
    mainImg: {
      type: Array,
      default: () => []
    }
  },
  setup () {
    const currIndex = ref(0)
    const target = ref(null) // target指定坐标系的范围
    // x/y 鼠标基于浏览器左上角
    // elementX 鼠标基于容器左上角X轴偏移
    // elementY 鼠标基于容器左上角Y轴偏移
    // isOutside 鼠标是否在模板容器外 true false
    // 三个参数都是ref对象
    const { elementX, elementY, isOutside } = useMouseInElement(target)
    // 滑块位置数据
    const left = ref(0) // 滑块距离左侧的距离
    const top = ref(0) // 滑块距离顶部的距离
    // 大图放大比例数据
    const bg = reactive({ x: 0, y: 0 })
    watch([elementX, elementY], () => {
      // 监听处理
      // 横向X
      if (elementX.value < 100) {
        // 左侧边界
        left.value = 0
      } else if (elementX.value > 300) {
        // 右侧边界
        left.value = 200
      } else {
        left.value = elementX.value - 100
      }
      // 纵向Y
      if (elementY.value < 100) {
        // 上侧边界
        top.value = 0
      } else if (elementY.value > 300) {
        // 下侧边界
        top.value = 200
      } else {
        top.value = elementY.value - 100
      }
      // 移动放大大图
      bg.x = -left.value * 2
      bg.y = -top.value * 2
    })

    return {
      currIndex,
      target,
      top,
      left,
      bg,
      isOutside
    }
  }
}
</script>
<style scoped lang="less">
.goods-image {
  width: 480px;
  height: 400px;
  position: relative;
  display: flex;
  .large {
    position: absolute;
    top: 0;
    left: 412px;
    width: 400px;
    height: 400px;
    z-index: 500;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
    background-repeat: no-repeat;
    background-size: 800px 800px;
    background-color: #f8f8f8;
  }
  .middle {
    width: 400px;
    height: 400px;
    background: #f5f5f5;
    .layer {
      width: 200px;
      height: 200px;
      background: rgba(0, 0, 0, 0.2);
      left: 0;
      top: 0;
      position: absolute;
    }
  }
  .small {
    width: 80px;
    li {
      width: 68px;
      height: 68px;
      margin-left: 12px;
      margin-bottom: 15px;
      cursor: pointer;
      &:hover,
      &.active {
        border: 2px solid @xtxColor;
      }
    }
  }
}
</style>
